﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.DataClass.Models;
using TheControllerRisk.Settings;

namespace RiskMan.DataClass
{
    public partial class Data 
    {
        /// <summary>
        /// возвращает точность по коду бумаги 
        /// </summary>
        public int GetAccuraccy(string seccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
                if (CurrentList[i].Seccode == seccode)
                    return CurrentList[i].Accurasy;

            return 0;
        }

        /// <summary>
        /// Возвращает код класса бумаги по коду бумаги
        /// </summary>
        public String GetClasscode(string seccode)
        {

            for (int i = 0; i < CurrentList.Count; i++)
                if (CurrentList[i].Seccode == seccode)
                    return CurrentList[i].Classcode;

            return "";
        }

        public double GetMaxMinPrice(string seccode, char op)
        {
            for (int i = 0; i < CurrentList.Count; i++)
                if (CurrentList[i].Seccode == seccode)
                {
                    if (op == 'S')
                        return CurrentList[i].MaxPrice;
                    if (op == 'B')
                        return CurrentList[i].MinPrice;
                }

            return 0;
        }

        /// <summary>
        /// Возвращает все полученные в систему счета  
        /// </summary>
        public List<Trade> GetTradeList()
        {
            List<Trade> result = new List<Trade>();
            for ( int i = 0; i < T2QItems.Count; i++ )
                result.AddRange( T2QItems [ i ].TradeList );
            return result;
        }

        public List<Trade> GetTradeAllList()
        {
            List<Trade> result = new List<Trade>();

            for (int i = 0; i < LimitebyAccount.Count; i++)
                if (LimitebyAccount[i].TypeLimit != cfg.ZALOG_MONEY &&
                    LimitebyAccount[i].TypeLimit != cfg.KLIRING_MONEY &&
                    LimitebyAccount[i].TypeLimit != cfg.KLIRING_ZALOG_MONEY)
                    result.Add(new Trade()
                    {
                        Account = LimitebyAccount[i].Account,
                        ClientCode = LimitebyAccount[i].Account
                    });
            for (int i = 0; i < EmitentLimiteList.Count; i++)
            {
                bool tr = false;
                for (int j = 0; j < result.Count; j++)
                    if (result[j].Account == EmitentLimiteList[i].Account &&
                        result[j].ClientCode == EmitentLimiteList[i].ClientCode)
                        tr = true;
                if (tr == false)
                    result.Add(new Trade()
                    {
                        Account = EmitentLimiteList[i].Account,
                        ClientCode = EmitentLimiteList[i].ClientCode
                    });
            }
            return result;
        }

        public List<String> GetSeccodeList()
        { 
            List<String> result = new List<string>() { cfg.UNKNOWN };

            for (int i = 0; i < CurrentList.Count; i++)
                result.Add(CurrentList[i].Seccode);

            return result;
        }


        /// <summary>
        /// массив UID
        /// </summary>
        /// <returns></returns>
        public List<String> GetUid()
        {
            List<String> result = new List<string>();

            for (int i = 0; i < StopOrderList.Count; i++)
            {
                bool tr = false;
                for (int j = 0; j < result.Count; i++)
                {
                    if (result[j] == StopOrderList[i].UID)
                        tr = true;
                }
                if (tr == false)
                    result.Add(StopOrderList[i].UID);
            }

            return result;
        }

        public double GetPrice(string sseccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
                if (CurrentList[i].Seccode == sseccode)
                    return CurrentList[i].LastPrice;
            return 0;
        }

        public double GetBalance(string account, string clienCode)
        {
            double result = this.GetTypeAccountMoney(account,clienCode);

            if (result == 0)
            {
                var ac = LimitebyAccount.Where(a => a.Account == account).ToList();
                for (int i = 0; i < ac.Count; i++)
                    if (ac[i].PlanEmptyPosition + ac[i].CurrentEmptyOpen + ac[i].CurrentEmptyOrder != 0)
                        return ac[i].PlanEmptyPosition + ac[i].CurrentEmptyOpen + ac[i].CurrentEmptyOrder;
            }
            return result;
        }

        public Double GetGO(string seccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
            {
                if (CurrentList[i].Seccode == seccode)
                {
                    if (CurrentList[i].Classcode != "SPBFUT" && CurrentList[i].Classcode != "SPBOPT")
                    {
                        return CurrentList[i].QUANTITY * CurrentList[i].LastPrice;
                    } 
                    return CurrentList[i].GOSell;
                }
            }
            return 0;
        }

        public Double GetLastPrice(String seccode)
        {
            try 
            {
                for (int i = 0; i < CurrentList.Count; i++)
                    if (CurrentList[i].Seccode == seccode)
                        return CurrentList[i].LastPrice;
            }
            catch (Exception)
            {

            }
            return 0;
        }
        public Double GetLastPrice ( String seccode, int pos )
        {
            try
            {
                for ( int i = 0; i < DealList.Count; i++ )
                {
                    if ( DealList [ i ].Seccode == seccode )
                    {
                        if ( DealList [ i ].Operation == ( pos > 0 ? cfg.BUY : cfg.SELL ) )
                            return DealList [ i ].Price;
                    }
                }
            }
            catch ( Exception )
            {

            }
            return 0;
        }
        public int GetCurPosition(String seccode, String account, String clientcode)
        {
            for ( int i = 0; i < FortsPositionsList.Count; i++ )
            {
                if ( FortsPositionsList [ i ].Account == account &&
                    FortsPositionsList [ i ].Seccode == seccode )
                    return FortsPositionsList [ i ].CurrentEmptyPos;
            }

            for ( int i = 0; i < EmitentLimiteList.Count; i++ )
            {
                if ( EmitentLimiteList [ i ].Account == account &&
                    EmitentLimiteList [ i ].ClientCode == clientcode &&
                    EmitentLimiteList [ i ].Seccode == seccode )
                    return EmitentLimiteList [ i ].CurrentOst;
            }

            return 0;
        }


        public int GetLots(string seccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
            {
                if (CurrentList[i].Seccode == seccode)
                    return CurrentList[i].QUANTITY;
            }
            return 1;
        }

        public string GetMarketPlace(string account, string clientcode)
        {
            return "FORTS";
        }

    }

    public enum MarketPlace 
    {
        NONE = 0,
        FORTS = 1,
        MMVB = 2
    }
}
